//
//  HYDeviceManagerView.m
//  TJHYHardwareProduct
//
//  Created by Kendama on 2018/1/23.
//  Copyright © 2018年 Kendama. All rights reserved.
//

#import "HYDeviceManagerView.h"
#import "HYFiltrateTableView.h"
@interface HYDeviceManagerView()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)NSArray *titleArray;

@end

@implementation HYDeviceManagerView

#pragma mark --- 点击事件
- (void)didClickFiltrateButton
{
    [UIView animateWithDuration:1 animations:^{
        if (_filtrateButton.selected)
        { // 收起
            [self.filtrateTableView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.bottom.mas_equalTo(self.filtrateButton.mas_bottom);
                make.left.right.mas_equalTo(self);
                make.height.mas_equalTo(px1920Height(1350));
            }];
            if (self.filtrateBlock)
            {
                self.filtrateBlock(12);
            }
        }
        else
        { // 展开
            [self.filtrateTableView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(self.filtrateButton.mas_top);
                make.left.right.mas_equalTo(self);
                make.height.mas_equalTo(px1920Height(1350));
            }];
            [self updateFiltrateTableView:@[@"",@"",@"",@"",@"",@"",@""]];
            
            if (self.filtrateBlock)
            {
                self.filtrateBlock(13);
            }
        }
    }];
    _filtrateButton.selected = !_filtrateButton.selected;
}

- (void)didClickCancelButton
{
    if (self.filtrateBlock)
    {
        self.filtrateBlock(10);
    }
}

- (void)hideFiltrateView
{
    [self.filtrateTableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.filtrateButton.mas_bottom);
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(px1920Height(1350));
    }];
    _filtrateButton.selected = NO;
}

- (void)didClickCommitButton
{
    [self.filtrateTableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.filtrateButton.mas_bottom);
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(px1920Height(1200));
    }];
    if (self.filtrateBlock)
    {
        self.filtrateBlock(11);
    }
    _filtrateButton.selected = NO;
}


#pragma mark --- 事件处理
- (void)updateFiltrateTableView:(NSArray *)array
{
    self.filtrateArray = array;
    [self.filtrateTableView.tableView reloadData];
}

#pragma mark -- 代理
#pragma mark - --------------------------UITableView dataSource--------------------------

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.titleArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"filtratecell"];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"filtratecell"];
    }
    cell.textLabel.font = fontWithSize(15);
    cell.detailTextLabel.font = fontWithSize(15);
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = self.titleArray[indexPath.row];
    cell.detailTextLabel.text = self.filtrateArray[indexPath.row];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}
#pragma mark - --------------------------UITableView delegate--------------------------
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return px1920Height(150);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.filtrateBlock)
    {
        self.filtrateBlock(indexPath.row);
    }
}

#pragma mark --- 懒加载
- (UIButton *)filtrateButton
{
    if(!_filtrateButton)
    {
        UIView *superView = self;
        _filtrateButton = [[UIButton alloc]init];
        [superView addSubview:_filtrateButton];
        [_filtrateButton addTarget:self action:@selector(didClickFiltrateButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _filtrateButton;
}

- (UIImageView *)downImageView
{
    if(!_downImageView)
    {
        _downImageView = [[UIImageView alloc]init];
        _downImageView.image = [UIImage imageNamed:@"drop-down"];
    }
    return _downImageView;
}


- (UITableView *)tableView
{
    if(!_tableView)
    {
        UIView *superView = self;
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        [superView addSubview:_tableView];
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
    }
    return _tableView;
}

- (HYFiltrateTableView *)filtrateTableView
{
    if(!_filtrateTableView)
    {
        UIView *superView = self;
        _filtrateTableView = [[HYFiltrateTableView alloc]init];
        [superView addSubview:_filtrateTableView];
        [_filtrateTableView.cancelButton addTarget:self action:@selector(didClickCancelButton) forControlEvents:UIControlEventTouchUpInside];
        [_filtrateTableView.commitButton addTarget:self action:@selector(didClickCommitButton) forControlEvents:UIControlEventTouchUpInside];
        _filtrateTableView.tableView.delegate = self;
        _filtrateTableView.tableView.dataSource = self;
    }
    return _filtrateTableView;
}

- (NSArray *)titleArray
{
    if (!_titleArray)
    {
        _titleArray = @[@"设备类型",@"地区",@"公司",@"分支机构",@"栋",@"层",@"房"];
    }
    return _titleArray;
}

- (NSArray *)filtrateArray
{
    if(!_filtrateArray)
    {
        _filtrateArray = @[@"",@"",@"",@"",@"",@"",@""];
    }
    return _filtrateArray;
}


#pragma mark -- 布局
- (void)masLayoutSubview
{
    __weak UIView *superView = self;
    superView.backgroundColor = rgba(245,245,245,1);
    
    UIView *headView = [[UIView alloc] init];
    headView.backgroundColor = [UIColor whiteColor];
    [superView addSubview:headView];
    
    [headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).with.offset(px1920Height(30));
        make.left.right.mas_equalTo(superView);
        make.height.mas_equalTo(px1920Height(120));
    }];
    
    UIButton *button = [[UIButton alloc] init];
    [button setImage:imageWithName(@"icon_screen") forState:UIControlStateNormal];
    [button setTitle:@"筛选" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    button.titleLabel.font = fontWithSize(15);
    [button layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:5];
    [headView addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(headView.mas_centerY);
        make.left.mas_equalTo(superView.mas_left).with.offset(px1080Width(66));
    }];
    
    [headView addSubview:self.downImageView];
    [self.downImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(headView.mas_centerY);
        make.right.mas_equalTo(headView.mas_right).with.offset(-px1080Width(56));
    }];
    
    [self.filtrateButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(headView);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(headView.mas_bottom).with.offset(px1920Height(30));
        make.left.right.mas_equalTo(superView);
        make.bottom.mas_equalTo(superView.mas_bottom);
    }];
    
    [self.filtrateTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.filtrateButton.mas_bottom);
        make.left.right.mas_equalTo(superView);
        make.height.mas_equalTo(px1920Height(1350));
    }];
    

    UIView *edgeView = [[UIView alloc] init];
    [superView addSubview:edgeView];
    edgeView.backgroundColor = rgba(245,245,245,1);
    [edgeView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top);
        make.height.mas_equalTo(px1920Height(30));
        make.left.right.mas_equalTo(superView);
    }];
    
    [self bringSubviewToFront:headView];
    [self bringSubviewToFront:self.filtrateButton];
    
}


@end
